package cn.gp.chatclient.ui.panel;

import cn.gp.chatclient.model.UserVo;
import cn.gp.chatclient.service.UserService;
import cn.gp.chatclient.ui.component.ContactListCellRenderer;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.function.Consumer;

/**
 * @ClassName ContactListPanel
 * @Description TODO
 * @Author He Guoping
 * @Date 2025/6/17 9:10
 * @Version JDK17
 */
@Slf4j
public class ContactListPanel extends JPanel {
    private UserService userService;
    private final JList<UserVo.FriendVo> contactList;
    private final DefaultListModel<UserVo.FriendVo> listModel;
    private final Consumer<UserVo.FriendVo> onContactSelected;

    public ContactListPanel(UserVo currentUser, Consumer<UserVo.FriendVo> onContactSelected) {
        this.onContactSelected = onContactSelected;
        
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);
        
        // 创建联系人列表
        listModel = new DefaultListModel<>();
        contactList = new JList<>(listModel);
        contactList.setCellRenderer(new ContactListCellRenderer());
        contactList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        contactList.setBackground(Color.WHITE);
        contactList.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // 添加双击事件监听器
        contactList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                if (evt.getClickCount() == 2) {
                    int index = contactList.locationToIndex(evt.getPoint());
                    if (index != -1) {
                        UserVo.FriendVo selectedFriend = listModel.getElementAt(index);
                        onContactSelected.accept(selectedFriend);
                    }
                }
            }
        });
        
        // 创建滚动面板
        JScrollPane scrollPane = new JScrollPane(contactList);
        scrollPane.setBorder(null);
        scrollPane.setBackground(Color.WHITE);
        
        // 添加标题和刷新按钮
        JPanel titlePanel = new JPanel(new BorderLayout());
        titlePanel.setBackground(Color.WHITE);
        titlePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        JLabel titleLabel = new JLabel("联系人列表");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
        titlePanel.add(titleLabel, BorderLayout.WEST);
        
        JButton refreshButton = new JButton("刷新");
        refreshButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        refreshButton.addActionListener(e -> refreshContacts());
        titlePanel.add(refreshButton, BorderLayout.EAST);
        
        add(titlePanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.CENTER);
        
        // 显示好友列表
        displayFriends(currentUser.getFriends());
    }
    
    public void refreshContacts() {
        if (userService.getCurrentUserId() == null) {
            log.warn("用户未登录，无法刷新联系人列表");
            return;
        }
        
        // 从服务器获取最新的好友列表
        userService.getCurrentUser().thenAccept(responseVo -> {
            if (responseVo.isSuccess()) {
                UserVo updatedUser = responseVo.getData();
                SwingUtilities.invokeLater(() -> {
                    displayFriends(updatedUser.getFriends());
                });
            } else {
                log.error("获取好友列表失败: {}", responseVo.getMessage());
            }
        }).exceptionally(throwable -> {
            log.error("获取好友列表异常", throwable);
            return null;
        });
    }
    
    private void displayFriends(List<UserVo.FriendVo> friends) {
        listModel.clear();
        if (friends != null) {
            for (UserVo.FriendVo friend : friends) {
                listModel.addElement(friend);
            }
        }
    }
    
//    private void openChatWindow(UserVo.FriendVo friend) {
//        SwingUtilities.invokeLater(() -> {
//            // 将FriendVo转换为UserVo
//            UserVo friendUser = new UserVo(
//                friend.getId(),
//                friend.getUsername(),
//                friend.getNickname(),
//                friend.getAvatar(),
//                friend.getStatus(),
//                null  // friends列表设为null，因为不需要
//            );
//            ChatFrame chatFrame = new ChatFrame(currentUser, friendUser);
//            chatFrame.setVisible(true);
//        });
//    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}




